﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AjaxPro;
using ECCIT;
using ECCIT.Web;
using ECCIT.Web.UI;
using ECCIT.Web.UI.WebControls;
using OgilvyOne.Sharan.Model;
using OgilvyOne.Sharan.BLL;
using OgilvyOne.Sharan.ProcessFlow;

namespace OgilvyOne.Sharan.Web
{
    [AjaxNamespace("KOL")]
    public partial class KOL : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(typeof(GridControl));
            Utility.RegisterTypeForAjax(typeof(WeiboUserBLL));

            if (!Page.IsPostBack)
            {
                AccountController accountController = new AccountController();
                Gridcontrol1.PageSize = 15;
            }
        }

        [AjaxMethod(HttpSessionStateRequirement.Read)]
        public ResultInfo Save(string unitXml)
        {
            WeiboUserModel unitInfo = GetUnitInfoFromXml(unitXml);
            WeiboUserBLL bll = new WeiboUserBLL();
            ResultInfo result = new ResultInfo(false);
            if (!unitInfo.ID.HasValue)
                result = bll.Save(unitInfo);

            return result;
        }

        private WeiboUserModel GetUnitInfoFromXml(string unitXml)
        {
            ModelDocument unitDoc = new ModelDocument(unitXml);
            string loginName = unitDoc.GetProperty("LoginName");
            string password = unitDoc.GetProperty("Password");
            long weiboUserID = long.Parse(unitDoc.GetProperty("WeiboUserID"));
            int age = int.Parse(unitDoc.GetProperty("Age"));
            string job = unitDoc.GetProperty("Job");
            string hobby = unitDoc.GetProperty("Hobby");

            WeiboUserModel unitInfo = new WeiboUserModel(null, loginName, password, weiboUserID, age, job, hobby);
            return unitInfo;
        }
    }
}